package com.etc.service.impl;

import com.etc.dao.WatercupMapper;
import com.etc.entity.Watercup;
import com.etc.entity.WatercupExample;
import com.etc.entity.WatercupKey;
import com.etc.service.IWaterCupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author kalista
 * @Description
 * @Date 2021/1/4  17:00
 **/
@Service
public class WaterCupServiceImpl implements IWaterCupService {


    @Autowired
    public WatercupMapper watercupMapper;

    @Override
    public Integer add(Watercup watercup) {

        try {
            int insert = watercupMapper.insert(watercup);
            return insert;
        }catch (Exception e){
            e.printStackTrace();
        }
       return null;
    }
    //    查询全部
    @Override
    public List<Watercup> findAll() {
        try {
            List<Watercup> waterCups = watercupMapper.selectByExample(null);
            return waterCups;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //根据ID找水杯
    @Override
    public Watercup getById(int wId) {
        try {
            WatercupKey watercupKey = new WatercupKey();
            watercupKey.setwId(wId);
            Watercup watercup = watercupMapper.selectByPrimaryKey(watercupKey);
            return watercup;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //根据name查水杯
    @Override
    public List<Watercup> findWaterCupByName(String wName) {
        try {
            WatercupExample watercupExample = new WatercupExample();
            WatercupExample.Criteria criteria = watercupExample.createCriteria();
            criteria.andWNameEqualTo(wName);
            List<Watercup> watercups = watercupMapper.selectByExample(watercupExample);
            return watercups;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
//根据ID删除
    @Override
    public Integer deleteById(Integer wId) {
        try {
        WatercupKey watercupKey = new WatercupKey();
        watercupKey.setwId(wId);
        int i = watercupMapper.deleteByPrimaryKey(watercupKey);
        return i;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
//更新
    @Override
    public Integer update(Watercup watercup) {
        try {
//           全改updateByPrimaryKey
        return watercupMapper.updateByPrimaryKeySelective(watercup);
//            watercupMapper.updateByExampleSelective(watercup)
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    }
